<template>
	<div class="scene-variables-container">
		<el-tabs v-model="activeName" @tab-click="handleClick">
			<el-tab-pane label="客户信息" name="customer" class="customer-box">
				<CustomerMessage
					:customerData="customerData"
					:roleData="roleData"
					:scenarioId="scenarioId"
					:activeTag="activeTag"
					@deleteRole="deleteRole"
					@addCustomerVariable="addCustomerVariable"
					@deleteCustomerVariable="deleteCustomerVariable"
					@editCustomerVariable="editCustomerVariable"
					@customerDetails="customerDetails"
					@changeCustomerPageSize="changeCustomerPageSize"
					@changeCustomerCurrentPage="changeCustomerCurrentPage"
					@checkRole="checkRole"
					@changeStatus="changeStatus"
				/>
				<!-- 客户信息新增变量组件 -->
				<AddVariable :dialogVisible="dialogVisible" :roleId="roleId" :activeTag="activeTag" v-if="dialogVisible" @handleClose="closeVariable" @sureOnok="sureOnok" />
				<EditVariable
					:showEditDialogVisible="showEditDialogVisible"
					:editRowId="editRowId"
					:activeTag="activeTag"
					:showErrorText="showErrorText"
					v-if="showEditDialogVisible"
					@editHandleClose="editHandleClose"
					@editSureOnok="editSureOnok"
					ref="editVariable"
				/>
				<VariableDetail :detailsDialogVisible="detailsDialogVisible" :variableDetailsData="variableDetailsData" :initMode="initCustomerMode" @closeVariableDetail="closeVariableDetail" />
			</el-tab-pane>
			<el-tab-pane label="业务信息" name="business">
				<BusinessMessage
					:businessData="businessData"
					@addBusinessVariable="addBusinessVariable"
					@deleteBusinessVariable="deleteBusinessVariable"
					@editBusinessVariable="editBusinessVariable"
					@businessDetails="businessDetails"
					@changeBusinessPageSize="changeBusinessPageSize"
					@changeBusinessCurrentPage="changeBusinessCurrentPage"
				/>
				<!-- 业务信息新增变量组件 -->
				<AddBusinessVariable :addBusinessDialog="addBusinessDialog" :roleId="roleId" v-if="addBusinessDialog" @colseAddBusiness="colseAddBusiness" @sureAddBusiness="sureAddBusiness" />
				<BuinessVariableDetail
					:businessDetailDialog="businessDetailDialog"
					:busDetailsId="busDetailsId"
					:scenarioId="scenarioId"
					v-if="businessDetailDialog"
					@colseAddBusiness="colseAddBusiness"
					@closeBusinessDetail="closeBusinessDetail"
				/>
				<BuinessEditVariable
					:buinessEditVariableDialog="buinessEditVariableDialog"
					:editRowId="editRowId"
					v-if="buinessEditVariableDialog"
					@colseEditBuinessVarible="colseEditBuinessVarible"
					@editBuinessVarible="editBuinessVarible"
				/>
			</el-tab-pane>
		</el-tabs>
		<el-button type="primary" class="back-btn" @click="goBack">返回</el-button>
	</div>
</template>

<script>
import CustomerMessage from './CustomerMessage.vue';
import BusinessMessage from './BusinessMessage.vue';
import AddVariable from './varComponents/AddVariable.vue';
import EditVariable from './varComponents/EditVariable.vue';
import VariableDetail from './varComponents/VariableDetail.vue';

import AddBusinessVariable from './varComponents/AddBusinessVariable.vue';
import BuinessVariableDetail from './varComponents/BuinessVariableDetail.vue';
import BuinessEditVariable from './varComponents/BuinessEditVariable.vue';
import {
	deldeteRole,
	getAllRole,
	addCustomerVariables,
	getCustomerVariablesDetails,
	updateCustomerVariablesById,
	deleteCustomerVariablesById,
	getCustomerVariablesByRole,
	getBusinessVariables,
	checkUsedVaribles
} from '@/api/api';
export default {
	name: '',
	components: { CustomerMessage, BusinessMessage, EditVariable, AddVariable, VariableDetail, BuinessVariableDetail, AddBusinessVariable, BuinessEditVariable },
	props: {},
	data() {
		return {
			activeName: 'customer',
			// 客户信息数据结构
			customerData: {
				sort: false,
				tableHead: [
					{
						label: '变量名',
						prop: 'variableName'
					},
					{
						label: '初始化方式',
						prop: 'initializationMode'
					},
					{
						label: '变量类型',
						prop: 'type'
					}
				],
				tableConent: [],
				showIndex: false,
				pageData: {
					page: 1,
					pageSize: 10,
					total: 0
				}
			},
			activeTag: 1,
			roleData: [],
			showEditDialogVisible: false,
			dialogVisible: false,
			activeSwitch: false,
			detailsDialogVisible: false, //变量详情
			variableDetailsData: {
				desensConfigMode: '', //脱敏配置模式
				variableName: '', //变量名
				describe: '', //变量描述
				initializationMode: 'user-data', //初始化方式
				type: 'text', //变量类型
				urlAddress: '', //URL地址
				callMode: 'get', //调用方式
				transferParameterVariable: [], //传参变量
				showSwitch: false,
				speechCheck: [], //AI播报话术脱敏规则  复选框
				subtitleCheck: [],
				chatmsgCheck: [],
				initializeField: ''
			},
			isShowField: true,
			//业务场景传过来的id
			scenarioId: JSON.parse(sessionStorage.getItem('sceneData'))?.id || this.$route.params.scenId,
			// 业务信息数据结构
			businessData: {
				sort: false,
				tableHead: [
					{
						label: '变量名',
						prop: 'variableName'
					},
					{
						label: '初始化方式',
						prop: 'initializationMode'
					},
					{
						label: '变量类型',
						prop: 'type'
					}
				],
				tableConent: [],
				showIndex: false,
				pageData: {
					page: 1,
					pageSize: 10,
					total: 0
				}
			},
			initCustomerMode: 'user-data',
			initBusinessMode: 'user-data',
			addBusinessDialog: false,
			editRowId: 0,
			businessDetailDialog: false,
			busDetailsId: 0,
			buinessEditVariableDialog: false,
			showErrorText: false
		};
	},
	computed: {},
	watch: {},
	created() {},
	mounted() {
		// console.warn(this.$route.params, 'activeType');
		if (this.$route.params.activeType) {
			this.activeName = this.$route.params.activeType;
		} else {
			this.activeName = 'customer';
		}
		this.initAllRole();
		this.initBusinessData();
	},
	methods: {
		handleClick(tab) {
			console.log(tab.name, 'handleClick:');
			if (tab.name === 'customer') {
				this.initAllRole();
				this.initcustomerData(this.activeTag);
			} else {
				this.initBusinessData();
			}
			console.log(this.activeTag);
		},
		goBack() {
			this.$router.go(-1);
		},
		// 分页
		changeCustomerPageSize(val) {
			this.customerData.pageData.pageSize = val;
			this.customerData.pageData.page = 1;
			this.initcustomerData(this.activeTag);
		},
		changeCustomerCurrentPage(val) {
			this.customerData.pageData.page = val;
			this.initcustomerData(this.activeTag);
		},
		// 客户信息相关方法
		checkRole(id) {
			console.log('activeTag', id);
			this.activeTag = id;
			this.customerData.pageData.page = 1;
			this.customerData.pageData.pageSize = 10;
			this.initcustomerData(this.activeTag);
		},
		deleteRole() {
			// console.log( this.activeTag);
			let data = {
				scenId: this.scenarioId,
				roleId: this.activeTag,
				variableIds: []
			};
			checkUsedVaribles(data)
				.then(() => {
					if (this.roleData.length === 1) {
						this.$message.warning('客户信息至少保留一个角色，无法进行删除！');
						console.log(this.roleData);
						this.initAllRole();
						return;
					}
					this.roleData.forEach(item => {
						console.log(item);
						if (item.id === this.activeTag) {
							console.log(item.name, 9999);
							this.$confirm(`确认删除角色<${item.name}>吗？ 删除后将无法恢复!`, '温馨提示', {
								confirmButtonText: '确定删除',
								cancelButtonText: '取消',
								type: 'warning'
							})
								.then(() => {
									deldeteRole(this.scenarioId, this.activeTag)
										.then(res => {
											console.log(res.data);
											if (res.data.code === 200) {
												this.$message.success('删除角色成功！');
												this.initAllRole();
											}
										})
										.catch(err => {
											console.log(err);
											if (err.code == '6009') {
												this.$message.error(`业务场景启用中，无法进行操作！`);
											}
										});
								})
								.catch(() => {});
						}
					});
				})
				.catch(err => {
					console.log(err);
					if (err.code === 5007) {
						this.$confirm('该角色下存在变量在AI工作流被引用，无法进行删除操作！', '温馨提示', {
							confirmButtonText: '确定',
							type: 'warning',
							showCancelButton: false
						}).catch(() => {});

						return;
					}
				});
		},
		addCustomerVariable() {
			this.dialogVisible = true;
			this.roleId = this.activeTag;
		},
		closeVariable() {
			this.dialogVisible = false;
		},
		sureOnok(form, selectedText) {
			console.log(selectedText, '父组件的form:', form);
			let scenId = this.scenarioId;
			let data = {
				name: form.variableName,
				describe: form.describe,
				initMode: form.initializationMode,
				linkedKey: form.initializeField ? form.initializeField : null,
				type: form.type,
				url: form.initializationMode === 'api-data' ? form.urlAddress : null,
				method: form.initializationMode === 'api-data' ? form.callMode : null,
				params: form.initializationMode === 'api-data' ? selectedText : [],
				roleId: this.activeTag,
				dataMasking: null
			};
			if (form.activeSwitch) {
				data.dataMasking = {
					type: form.desensConfigMode,
					enabled: form.activeSwitch,
					applyTo: {
						speech: form.speechCheck === '不脱敏' ? 0 : form.desensConfigMode === 'idCard' ? this.getIdCardPublic(form.speechCheck) : form.speechCheck,
						chatmsg: form.chatmsgCheck === '不脱敏' ? 0 : form.desensConfigMode === 'idCard' ? this.getIdCardPublic(form.chatmsgCheck) : form.chatmsgCheck,
						subtitle: form.subtitleCheck === '不脱敏' ? 0 : form.desensConfigMode === 'idCard' ? this.getIdCardPublic(form.subtitleCheck) : form.subtitleCheck
					}
				};
			}
			console.warn(data, 'sureOnok添加变量确认');
			addCustomerVariables(scenId, data)
				.then(res => {
					console.log(res.data, 'addCustomerVariables');
					this.initcustomerData(this.activeTag);
					this.dialogVisible = false;
					form.activeSwitch = false;
					// 点确定按钮弹框隐藏， 清除输入框的数据
					form.desensConfigMode = '';
					form.variableName = '';
					form.describe = '';
					form.transferParameterVariable = '';
					form.callMode = 'get';
					form.urlAddress = '';
					form.desensConfigMode = '';
					// 脱敏配置模式 输入框默认不脱敏
					form.speechRadio = '不脱敏';
					form.speechCheck = '不脱敏';
					form.chatmsgRadio = '不脱敏';
					form.chatmsgCheck = '不脱敏';
					form.subtitleRadio = '不脱敏';
					form.subtitleCheck = '不脱敏';
					form.speechCheck = '不脱敏';
					form.subtitleCheck = '不脱敏';
					form.chatmsgCheck = '不脱敏';
					form.disabledSelest = true;
				})
				.catch(err => {
					console.log(err);
					if (err.code == '6009') {
						this.$message.error(`业务场景启用中，无法进行操作！`);
					} else {
						this.$message.error('新增变量失败！');
					}
					form.activeSwitch = false;
					form.disabledSelest = false;
				});
		},
		deleteCustomerVariable(row) {
			console.log('deleteCustomerVariable', row);
			let data = {
				scenId: this.scenarioId,
				variableIds: [row.id],
				roleId: this.activeTag
			};
			checkUsedVaribles(data)
				.then(() => {
					this.initcustomerData(this.activeTag);
					this.$confirm(`确认删除变量<${row.variableName}>吗？ 删除后将无法恢复!`, '提示', {
						confirmButtonText: '确定删除',
						cancelButtonText: '取消',
						type: 'warning'
					})
						.then(() => {
							deleteCustomerVariablesById(this.scenarioId, row.id)
								.then(res => {
									console.log(res.data);
									if (res.data.code === 200) {
										this.$message.success('删除变量成功！');
										this.initcustomerData(this.activeTag);
									}
								})
								.catch(error => {
									console.log(error);
									if (error.code == '6009') {
										this.$message.error(`业务场景启用中，无法进行操作！`);
									}
								});
						})
						.catch(() => {});
				})
				.catch(err => {
					console.log(err);
					if (err.code === 5007) {
						this.$confirm('该变量在AI工作流中被引用，无法进行删除操作！', '温馨提示', {
							confirmButtonText: '确定',
							showCancelButton: false,
							type: 'warning'
						}).catch(() => {});
						return;
					}
				});
		},
		editCustomerVariable(row) {
			this.editRowId = row.id;
			this.showEditDialogVisible = true;
			console.log('editCustomerVariable', row);
		},
		editHandleClose() {
			this.showEditDialogVisible = false;
			this.showErrorText = false;
		},
		editSureOnok(form, selectedText) {
			console.log(selectedText, 'editSureOnok:', form);
			let data = {
				name: form.variableName,
				initMode: form.initializationMode,
				linkedKey: form.initializationMode === 'api-data' || form.initializationMode === 'user-data' ? form.initializeField : null,
				type: form.type,
				url: form.urlAddress || null,
				method: form.initializationMode === 'api-data' ? form.callMode : '',
				params: selectedText.length > 0 ? selectedText : null,
				roleId: this.activeTag,
				dataMasking: {}
			};
			if (form.showSwitch) {
				data.dataMasking = {
					type: form.desensConfigMode,
					enabled: form.showSwitch,
					applyTo: {
						speech:
							form.speechCheck === '不脱敏'
								? 0
								: form.desensConfigMode === 'idCard'
								? this.getIdCardPublic(form.speechCheck)
								: typeof form.speechCheck === 'string'
								? this.getParamsDataMasking(form.desensConfigMode, form.speechCheck)
								: form.speechCheck,
						chatmsg:
							form.chatmsgCheck === '不脱敏'
								? 0
								: form.desensConfigMode === 'idCard'
								? this.getIdCardPublic(form.chatmsgCheck)
								: typeof form.chatmsgCheck === 'string'
								? this.getParamsDataMasking(form.desensConfigMode, form.chatmsgCheck)
								: form.chatmsgCheck,
						subtitle:
							form.subtitleCheck === '不脱敏'
								? 0
								: form.desensConfigMode === 'idCard'
								? this.getIdCardPublic(form.subtitleCheck)
								: typeof form.subtitleCheck === 'string'
								? this.getParamsDataMasking(form.desensConfigMode, form.subtitleCheck)
								: form.subtitleCheck
					}
				};
			}
			console.log(data, '传参');
			updateCustomerVariablesById(this.scenarioId, this.editRowId, data)
				.then(res => {
					console.log(res.data);
					this.$message.success('修改变量成功！');
					this.initcustomerData(this.activeTag);
					this.showEditDialogVisible = false;
					this.showErrorText = false;
				})
				.catch(err => {
					if (err.code === 5011 && err.data.existType === 1) {
						console.log(err, '5011', this.$refs.editVariable.showErrorText);
						this.showErrorText = true;
						this.$refs.editVariable.showErrorText = true;
						this.$refs.editVariable.defaultDisable = false;
					} else if (err.code == '6009') {
						this.showErrorText = false;
						this.$message.error(`业务场景启用中，无法进行操作！`);
					} else {
						this.showErrorText = false;
						this.$message.error('修改变量失败！');
					}
				});
		},
		customerDetails(row) {
			console.log('customerDetails:', row);
			this.detailsDialogVisible = true;
			getCustomerVariablesDetails(this.scenarioId, row.id)
				.then(res => {
					const { name, dataMasking, linkedKey } = res.data.data;
					console.log(name, linkedKey, row.method);
					this.variableDetailsData.variableName = row.variableName || name;
					this.variableDetailsData.initializationMode = row.initializationMode;
					this.variableDetailsData.initializeField = linkedKey;
					this.variableDetailsData.type = row.type;
					this.variableDetailsData.urlAddress = row.url;
					this.variableDetailsData.callMode = row.method;
					// this.variableDetailsData.transferParameterVariable = row.params;
					if (row.params) {
						this.variableDetailsData.transferParameterVariable = row.params.map(item => {
							// console.log(item);
							if (item.roleId && item.roleName) {
								return `客户信息-${item.roleName}-${item.name}`;
							}
							return `业务信息-${item.name}`;
						});
					}
					// console.log(this.variableDetailsData.transferParameterVariable, 'transferParameterVariable');
					this.initCustomerMode = row.initMode;

					// 脱敏配置 数据回显
					if (dataMasking && dataMasking.applyTo) {
						console.log('dataMasking :', dataMasking);
						this.variableDetailsData.showSwitch = dataMasking.enabled;
						this.variableDetailsData.desensConfigMode = this.getdesensConfigType(dataMasking.type);
						this.variableDetailsData.speechCheck = this.getDetailSpeech(dataMasking.applyTo.speech);
						this.variableDetailsData.chatmsgCheck = this.getDetailChatmsg(dataMasking.applyTo.chatmsg);
						this.variableDetailsData.subtitleCheck = this.getDetailSubtitle(dataMasking.applyTo.subtitle);
					} else {
						this.variableDetailsData.showSwitch = false;
					}
					if (linkedKey) {
						this.variableDetailsData.initializeField = linkedKey;
					} else {
						this.isShowField = false;
					}
				})
				.catch(err => {
					console.log(err);

					this.$message.error('获取变量详情失败！');
				});
		},
		closeVariableDetail() {
			this.detailsDialogVisible = false;
		},
		// 客户信息变量配置初始化
		initcustomerData(id) {
			let data = {
				scenId: this.scenarioId,
				roleId: id || this.activeTag,
				pageSize: this.customerData.pageData.pageSize,
				page: this.customerData.pageData.page
			};
			getCustomerVariablesByRole(data)
				.then(res => {
					console.log(res.data.data, 'initcustomerData');
					this.customerData.tableConent = res.data.data.data.map(item => {
						return {
							id: item.id,
							variableName: item.name,
							roleId: item.roleId,
							initMode: item.initMode,
							initializationMode: item.initMode === 'user-data' ? '随路数据' : item.initMode === 'api-data' ? 'API获取' : '非初始化',
							type: item.type === 'text' ? '文本/字符串' : item.type === 'numeric' ? '数值' : '数字串',
							dataMasking: item.dataMasking ? item.dataMasking : '',
							params: item.params,
							url: item.url,
							method: item.method,
							linkedKey: item.linkedKey,
							isDefault: item.isDefault,
							isDisabled: item.isDisabled
						};
					});
					this.customerData.pageData.total = res.data.data.count;
				})
				.catch(err => {
					console.log(err);
					this.$message.error('获取客户信息变量列表失败！');
				});
		},
		// 业务信息相关方法
		addBusinessVariable() {
			console.log('addBusinessVariable:');
			this.addBusinessDialog = true;
		},
		colseAddBusiness() {
			this.addBusinessDialog = false;
		},
		sureAddBusiness(form, selectedText) {
			console.log('form:', form);
			let scenId = this.scenarioId;
			let data = {
				name: form.variableName,
				describe: form.describe,
				initMode: form.initializationMode,
				linkedKey: form.initializeField ? form.initializeField : null,
				type: form.type,
				url: form.initializationMode === 'api-data' ? form.urlAddress : null,
				method: form.initializationMode === 'api-data' ? form.callMode : null,
				params: selectedText?.length > 0 ? selectedText : null,
				roleId: null
			};
			if (form.activeSwitch) {
				data.dataMasking = {
					type: form.desensConfigMode,
					enabled: form.activeSwitch,
					applyTo: {
						speech: form.speechCheck === '不脱敏' ? 0 : form.desensConfigMode === 'idCard' ? this.getIdCardPublic(form.speechCheck) : form.speechCheck,
						chatmsg: form.chatmsgCheck === '不脱敏' ? 0 : form.desensConfigMode === 'idCard' ? this.getIdCardPublic(form.chatmsgCheck) : form.chatmsgCheck,
						subtitle: form.subtitleCheck === '不脱敏' ? 0 : form.desensConfigMode === 'idCard' ? this.getIdCardPublic(form.subtitleCheck) : form.subtitleCheck
					}
				};
				console.log(data.dataMasking);
			}
			addCustomerVariables(scenId, data)
				.then(() => {
					this.initBusinessData();
					this.addBusinessDialog = false;
					form.activeSwitch = false;
					// 点确定按钮弹框隐藏， 清除输入框的数据
					form.desensConfigMode = '';
					form.variableName = '';
					form.describe = '';
					form.transferParameterVariable = '';
					form.callMode = 'get';
					form.urlAddress = '';
					form.desensConfigMode = '';
					// 脱敏配置模式 输入框默认不脱敏
					form.speechRadio = '不脱敏';
					form.speechCheck = '不脱敏';
					form.chatmsgRadio = '不脱敏';
					form.chatmsgCheck = '不脱敏';
					form.subtitleRadio = '不脱敏';
					form.subtitleCheck = '不脱敏';
					form.speechCheck = '不脱敏';
					form.subtitleCheck = '不脱敏';
					form.chatmsgCheck = '不脱敏';
					form.disabledSelest = true;
				})
				.catch(err => {
					console.log(err);
					if (err.code == '6009') {
						this.$message.error(`业务场景启用中，无法进行操作！`);
					} else {
						this.$message.error('新增变量失败！');
					}
					form.activeSwitch = false;
					form.disabledSelest = false;
				});
		},
		deleteBusinessVariable(row) {
			console.log('deleteBusinessVariable:', row);
			this.$confirm(`确认删除变量<${row.variableName}>吗？ 删除后将无法恢复!`, '提示', {
				confirmButtonText: '确定删除',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					deleteCustomerVariablesById(this.scenarioId, row.id)
						.then(res => {
							console.log(res, 9999);
							if (res.data.code === 200) {
								this.$message.success('删除变量成功！');
								this.initBusinessData();
							}
						})
						.catch(err => {
							console.log(err);
							if (err.code === 5007) {
								this.$confirm('该变量在AI工作流中被引用，无法进行删除操作！', '温馨提示', {
									confirmButtonText: '确定',
									showCancelButton: false,
									type: 'warning'
								}).catch(() => {});
							} else if (err.code == '6009') {
								this.$message.error(`业务场景启用中，无法进行操作！`);
							}
						});
				})
				.catch(() => {});
		},
		editBusinessVariable(row) {
			console.log('editBusinessVariable:', row);
			this.editRowId = row.id;
			this.buinessEditVariableDialog = true;
		},
		editBuinessVarible(form, selectedText) {
			let data = {
				name: form.variableName,
				describe: form.describe,
				initMode: form.initializationMode,
				linkedKey: form.initializeField ? form.initializeField : null,
				type: form.type,
				url: form.urlAddress || null,
				method: form.initializationMode === 'api-data' ? form.callMode : '',
				params: form.initializationMode === 'api-data' ? selectedText : null,
				roleId: null,
				dataMasking: {}
			};
			if (form.showSwitch) {
				data.dataMasking = {
					type: form.desensConfigMode,
					enabled: form.showSwitch,
					applyTo: {
						speech:
							form.speechCheck === '不脱敏'
								? 0
								: form.desensConfigMode === 'idCard'
								? this.getIdCardPublic(form.speechCheck)
								: typeof form.speechCheck === 'string'
								? this.getParamsDataMasking(form.desensConfigMode, form.speechCheck)
								: form.speechCheck,
						chatmsg:
							form.chatmsgCheck === '不脱敏'
								? 0
								: form.desensConfigMode === 'idCard'
								? this.getIdCardPublic(form.chatmsgCheck)
								: typeof form.chatmsgCheck === 'string'
								? this.getParamsDataMasking(form.desensConfigMode, form.chatmsgCheck)
								: form.chatmsgCheck,
						subtitle:
							form.subtitleCheck === '不脱敏'
								? 0
								: form.desensConfigMode === 'idCard'
								? this.getIdCardPublic(form.subtitleCheck)
								: typeof form.subtitleCheck === 'string'
								? this.getParamsDataMasking(form.desensConfigMode, form.subtitleCheck)
								: form.subtitleCheck
					}
				};
				console.log(data.dataMasking);
			}
			console.warn(form, 'editBuinessVarible', data);
			updateCustomerVariablesById(this.scenarioId, this.editRowId, data)
				.then(res => {
					console.log(res.data);
					this.$message.success('修改变量成功！');
					this.buinessEditVariableDialog = false;
					this.initBusinessData();
				})
				.catch(err => {
					console.log(err);
					if (err.code == '6009') {
						this.$message.error(`业务场景启用中，无法进行操作！`);
					} else{
						this.$message.error('修改变量失败！');
					}
					this.buinessEditVariableDialog = false;
				});
		},
		colseEditBuinessVarible() {
			this.buinessEditVariableDialog = false;
		},
		businessDetails(row) {
			console.log('businessDetails', row);
			this.busDetailsId = row.id;
			this.businessDetailDialog = true;
		},
		closeBusinessDetail(row) {
			console.log('businessDetails', row);
			this.businessDetailDialog = false;
		},
		// 分页
		changeBusinessPageSize(val) {
			this.businessData.pageData.pageSize = val;
			this.businessData.pageData.page = 1;
			this.initBusinessData();
		},
		changeBusinessCurrentPage(val) {
			this.businessData.pageData.page = val;
			this.initBusinessData();
		},
		// 客户信息变量配置初始化
		initBusinessData() {
			getBusinessVariables({ scenId: this.scenarioId, page: this.businessData.pageData.page, pageSize: this.businessData.pageData.pageSize })
				.then(res => {
					console.log(res.data.data, 'initBusinessData');
					this.businessData.tableConent = res.data.data.data.map(item => {
						return {
							id: item.id,
							variableName: item.name,
							roleId: item.roleId,
							initializationMode: item.initMode === 'user-data' ? '随路数据' : item.initMode === 'api-data' ? 'API获取' : '非初始化',
							type: item.type === 'text' ? '文本/字符串' : item.type === 'numeric' ? '数值' : '数字串',
							dataMasking: item.dataMasking ? item.dataMasking : '',
							params: item.params,
							url: item.url,
							method: item.method,
							linkedKey: item.linkedKey,
							isDefault: item.isDefault
						};
					});
					this.businessData.pageData.total = res.data.data.count;
				})
				.catch(err => {
					console.log(err);
					this.$message.error('获取客户信息变量列表失败！');
				});
		},
		// 脱敏传递的接口参数  dataMasking 字段 脱敏选项
		//  获取身份证号脱敏 select 选择的值， 默认是个数组 转成数字传给后台
		getIdCardPublic(value) {
			console.log('value', value);
			let result = 0;
			if (value.length === 4) {
				result = 15;
			} else if (value.length === 1) {
				value.forEach(item => {
					if (item === '脱敏地址区') {
						result = 1;
					} else if (item === '脱敏顺序号') {
						result = 2;
					} else if (item === '脱敏生日区') {
						result = 4;
					} else if (item === '脱敏生日月日区') {
						result = 8;
					}
					console.log('idCardNumber :', result);
				});
			} else if (value.length === 2) {
				if (value.includes('脱敏生日区') && value.includes('脱敏生日月日区')) {
					result = 12;
					console.log('4+8 :', result);
				}
				if (value.includes('脱敏地址区') && value.includes('脱敏顺序号')) {
					result = 3;
					console.log('1+2 :', result);
				}
				if (value.includes('脱敏地址区') && value.includes('脱敏生日月日区')) {
					result = 9;
					console.log('1+8 :', result);
				}
				if (value.includes('脱敏顺序号') && value.includes('脱敏生日区')) {
					result = 5;
					console.log('1+4 :', result);
				}
				if (value.includes('脱敏顺序号') && value.includes('脱敏生日区')) {
					result = 6;
					console.log('2+4 :', result);
				}
				if (value.includes('脱敏顺序号') && value.includes('脱敏生日月日区')) {
					result = 10;
					console.log('2+8 :', result);
				}
			} else if (value.length === 3) {
				if (value.includes('脱敏地址区') && value.includes('脱敏生日区') && value.includes('脱敏生日月日区') && !value.includes('脱敏顺序号')) {
					result = 13;
					console.log('1+8+4 :', result);
					return result;
				}
				if (value.includes('脱敏生日区') && value.includes('脱敏生日月日区') && value.includes('脱敏顺序号') && !value.includes('脱敏地址区')) {
					result = 14;
					console.log('4+8+2 :', result);
					return result;
				}
				return result;
			} else {
				result = 15;
				console.log('1+4+8+2 :', result);
				return result;
			}
			console.log('idCardSpeechNumber:', result);
			return result;
		},

		initAllRole() {
			getAllRole(this.scenarioId)
				.then(res => {
					this.roleData = res.data.data;
					this.activeTag = res.data.data[0]?.id;
					if (this.activeTag) {
						this.initcustomerData(this.activeTag);
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 默认变量 姓名，身份证号，头像，性别，停启用
		async changeStatus(row) {
			try {
				// row.isDisabled = !row.isDisabled;
				console.log(row.isDisabled, 'changeStatus', row);
				let params = {
					isDisabled: row.isDisabled === true ? false : true
				};
				const { data } = await updateCustomerVariablesById(this.scenarioId, row.id, params);
				console.log(data);
				if (data.code === 200) {
					if (row.isDisabled) {
						row.isDisabled = false;
						this.$message.success(`<${row.variableName}>已启用`);
					} else {
						row.isDisabled = true;
						this.$message.success(`<${row.variableName}>已停用`);
					}
					this.initcustomerData(this.activeTag);
				} else {
					console.log('changeStatus function error', data);
				}
			} catch (error) {
				console.log(error);
				if (error.code === 5012) {
					row.isDisabled = false;
					this.$message.error(`默认变量不能停用！`);
				} else if (error.code === 5007) {
					// 接口默认返回的是 false
					row.isDisabled = false;
					this.$message.error(`<${row.variableName}>在AI工作流中被引用，无法进行停用！`);
				} else if (error.code == '6009') {
					row.isDisabled = false;
					this.$message.error(`业务场景启用中，无法进行操作！`);
				}
			}
		},

		// 脱敏配置模式  详情数据回显
		getdesensConfigType(value) {
			let result = '';
			switch (value) {
				case 'userName':
					result = '姓名';
					break;
				case 'idCard':
					result = '身份证号';
					break;
				case 'phoneNumber':
					result = '手机号';
					break;
				case 'homeAddress':
					result = '家庭地址';
					break;
				default:
					result = '';
					break;
			}
			return result;
		},
		getDetailSpeech(value) {
			let result = '';
			if (this.variableDetailsData.desensConfigMode === '姓名') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留首字';
						break;
					case 2:
						result = '保留末字';
						break;
					case 3:
						result = '首末各留一字';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '身份证号') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 15:
						result = '脱敏地址区 脱敏生日区 脱敏生日月日区 脱敏顺序号';
						break;
					case 1:
						result = '脱敏地址区';
						break;
					case 2:
						result = '脱敏顺序号';
						break;
					case 4:
						result = '脱敏生日区';
						break;
					case 8:
						result = '脱敏生日月日区';
						break;
					case 3:
						result = '脱敏地址区 脱敏顺序号';
						break;
					case 9:
						result = '脱敏地址区 脱敏生日月日区';
						break;
					case 13:
						result = '脱敏地址区 脱敏生日区 脱敏生日月日区';
						break;
					case 5:
						result = '脱敏地址区 脱敏生日区';
						break;
					case 6:
						result = '脱敏顺序号 脱敏生日区';
						break;
					case 10:
						result = '脱敏顺序号 脱敏生日月日区';
						break;
					case 12:
						result = '脱敏生日区 脱敏生日月日区';
						break;
					case 14:
						result = '脱敏生日区 脱敏生日月日区 脱敏顺序号';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '手机号') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留前三位';
						break;
					case 2:
						result = '保留后四位';
						break;
					case 3:
						result = '脱敏中间四位';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '家庭地址') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留首字';
						break;
					case 2:
						result = '保留末字';
						break;
					case 3:
						result = '首末各留一字';
						break;
					default:
						result = '';
						break;
				}
				return result;
			}
			console.log('speechCheck:', result);
			console.log(this.variableDetailsData);
			return result;
		},
		getDetailSubtitle(value) {
			let result = '';
			if (this.variableDetailsData.desensConfigMode === '姓名') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留首字';
						break;
					case 2:
						result = '保留末字';
						break;
					case 3:
						result = '首末各留一字';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '身份证号') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 15:
						result = '脱敏地址区 脱敏生日区 脱敏生日月日区 脱敏顺序号';
						break;
					case 1:
						result = '脱敏地址区';
						break;
					case 2:
						result = '脱敏顺序号';
						break;
					case 4:
						result = '脱敏生日区';
						break;
					case 8:
						result = '脱敏生日月日区';
						break;
					case 3:
						result = '脱敏地址区 脱敏顺序号';
						break;
					case 9:
						result = '脱敏地址区 脱敏生日月日区';
						break;
					case 13:
						result = '脱敏地址区 脱敏生日区 脱敏生日月日区';
						break;
					case 5:
						result = '脱敏地址区 脱敏生日区';
						break;
					case 6:
						result = '脱敏顺序号 脱敏生日区';
						break;
					case 10:
						result = '脱敏顺序号 脱敏生日月日区';
						break;
					case 12:
						result = '脱敏生日区 脱敏生日月日区';
						break;
					case 14:
						result = '脱敏生日区 脱敏生日月日区 脱敏顺序号';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '手机号') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留前三位';
						break;
					case 2:
						result = '保留后四位';
						break;
					case 3:
						result = '脱敏中间四位';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '家庭地址') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留首字';
						break;
					case 2:
						result = '保留末字';
						break;
					case 3:
						result = '首末各留一字';
						break;
					default:
						result = '';
						break;
				}
				return result;
			}
			console.log('speechCheck:', result);
			console.log(this.variableDetailsData);
			return result;
		},
		getDetailChatmsg(value) {
			let result = '';
			if (this.variableDetailsData.desensConfigMode === '姓名') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留首字';
						break;
					case 2:
						result = '保留末字';
						break;
					case 3:
						result = '首末各留一字';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '身份证号') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 15:
						result = '脱敏地址区 脱敏生日区 脱敏生日月日区 脱敏顺序号';
						break;
					case 1:
						result = '脱敏地址区';
						break;
					case 2:
						result = '脱敏顺序号';
						break;
					case 4:
						result = '脱敏生日区';
						break;
					case 8:
						result = '脱敏生日月日区';
						break;
					case 3:
						result = '脱敏地址区 脱敏顺序号';
						break;
					case 9:
						result = '脱敏地址区 脱敏生日月日区';
						break;
					case 13:
						result = '脱敏地址区 脱敏生日区 脱敏生日月日区';
						break;
					case 5:
						result = '脱敏地址区 脱敏生日区';
						break;
					case 6:
						result = '脱敏顺序号 脱敏生日区';
						break;
					case 10:
						result = '脱敏顺序号 脱敏生日月日区';
						break;
					case 12:
						result = '脱敏生日区 脱敏生日月日区';
						break;
					case 14:
						result = '脱敏生日区 脱敏生日月日区 脱敏顺序号';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '手机号') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留前三位';
						break;
					case 2:
						result = '保留后四位';
						break;
					case 3:
						result = '脱敏中间四位';
						break;
					default:
						result = '';
						break;
				}
				return result;
			} else if (this.variableDetailsData.desensConfigMode === '家庭地址') {
				switch (value) {
					case 0:
						result = '不脱敏';
						break;
					case 4:
						result = '全脱敏';
						break;
					case 1:
						result = '保留首字';
						break;
					case 2:
						result = '保留末字';
						break;
					case 3:
						result = '首末各留一字';
						break;
					default:
						result = '';
						break;
				}
				return result;
			}
			console.log('speechCheck:', result);
			console.log(this.variableDetailsData);
			return result;
		},
		// 修改传递给后端的参数
		getParamsDataMasking(type, value) {
			console.warn(type, value, 'getParamsDataMasking');
			let result = '';
			if (type === 'userName') {
				switch (value) {
					case '不脱敏':
						result = 0;
						break;
					case '全脱敏':
						result = 4;
						break;
					case '保留首字':
						result = 1;
						break;
					case '保留末字':
						result = 2;
						break;
					case '首末各留一字':
						result = 3;
						break;
					default:
						result = 0;
						break;
				}
			} else if (type === 'phoneNumber') {
				switch (value) {
					case '不脱敏':
						result = 0;
						break;
					case '全脱敏':
						result = 4;
						break;
					case '保留前三位':
						result = 1;
						break;
					case '保留后四位':
						result = 2;
						break;
					case '脱敏中间四位':
						result = 3;
						break;
					default:
						result = '';
						break;
				}
			} else if (type === 'homeAddress') {
				switch (value) {
					case '不脱敏':
						result = 0;
						break;
					case '全脱敏':
						result = 4;
						break;
					case '保留首字':
						result = 1;
						break;
					case '保留末字':
						result = 2;
						break;
					case '首末各留一字':
						result = 3;
						break;
					default:
						result = 0;
						break;
				}
			}
			console.log('getParamsDataMasking:', result);
			return result;
		}
	}
};
</script>

<style lang="less" scoped>
.scene-variables-container {
	position: relative;

	.back-btn {
		// height: 28px;
		background: #6384fe;
		border-radius: 4px;
		line-height: 3px;
		position: absolute;
		top: 0;
		right: 20px;
	}
}
</style>
<style lang="less">
.scene-variables-container .el-tabs__active-bar {
	background-color: #6384fe !important;
}
.scene-variables-container .el-tabs__item {
	font-size: 15px;
	font-family: PingFang SC;
	font-weight: 400;
	color: #b7b7b7 !important;
}
.el-tabs__item.is-active {
	color: #6384fe !important;
}
</style>
